package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @Author FJL
 * @Description TODO
 * @Date 2022/8/27 16:16
 **/
public class UserJob {

@Autowired
private UserService userService;
@Autowired
private RedisTemplate redisTemplate;

@XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param){
        XxlJobLogger.log("每日工单量列表初始化");
        userService.list().forEach(user -> {
            if (!user.getRoleCode().equals("1001")) { // 不考虑管理员
                String key = VMSystem.REGION_TASK_KEY_PREF // 前缀
                        + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                        +"."+ user.getRegionId() // 区域
                        +"." + user.getRoleCode();
                redisTemplate.opsForZSet().add(key,user.getId(),0); // 添加元素到变量中，同时添加分值
                redisTemplate.expire(key, Duration.ofDays(1));  // 在redis中存放的时间
            }
        });
        return ReturnT.SUCCESS;
    }

}

